import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xiaotutu
 * Date: 2024-06-11
 * Time: 14:04
 */
public class Test {

    public static void main4(String[] args) {
        Deque<Integer> deque = new LinkedList<>();
        deque.offer(1);
        Deque<Integer> stack = new ArrayDeque<>();
        stack.push(1);
        Deque<Integer> queue = new LinkedList<>();
    }

    public static void main3(String[] args) {
        // Queue<Integer> queue = new LinkedList<>();
        // queue.offer(1);
        MyQueue myQueue = new MyQueue();
        myQueue.offer(1);
        myQueue.offer(2);
        myQueue.offer(3);
        myQueue.poll();
        myQueue.poll();
        System.out.println(myQueue.peek());
    }

    public static boolean IsPopOrder (int[] pushV, int[] popV) {
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for(int i = 0; i < pushV.length; i++) {
            stack.push(pushV[i]);
            while(!stack.empty() && j < popV.length && stack.peek() == popV[j]) {
                stack.pop();
                j++;
            }
        }
        return stack.empty();

    }

    public static void main2(String[] args) {
        Stack<Integer> stack = new Stack();
        int[] pshv = new int[] {2, 1 ,0};
        int[] popV = new int[] {1, 2, 0};
        System.out.println(IsPopOrder(pshv, popV));
    }

    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if(ch == '[' || ch == '{' || ch == '(') {
                //左括号入栈
                stack.push(ch);
            }else {
                //右括号匹配
                if(stack.empty()) {
                    return false;
                }else {
                    char tmp = stack.peek();
                    if(tmp == '[' && ch == ']' || tmp == '{' && ch == '}' || tmp == '(' && ch == ')') {
                        stack.pop();
                    }else {
                        return false;
                    }
                }
            }
        }
        if(!stack.empty()) {
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        boolean ret = isValid("(]");
        System.out.println(ret);
    }

    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();

        for(String s : tokens) {
            if(!isOpera(s)) {
                // 数字 放入栈中
                stack.push(Integer.parseInt(s));
            }else {
                // 弹出栈顶的两个操作数
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch(s){
                    case "+":
                        stack.push(num1 + num2);
                        break;
                    case "-":
                        stack.push(num1 - num2);
                        break;
                    case "*":
                        stack.push(num1 * num2);
                        break;
                    case "/":
                        stack.push(num1 / num2);
                        break;
                }
            }
        }
        return stack.pop();
    }

    public boolean isOpera(String s) {
        if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/") ) {
            return true;
        }
        return false;
    }
    public static void main1(String[] args) {
        Stack<Integer> stack = new Stack();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        System.out.println(stack.size()); // 获取栈中有效元素个数---> 4
        System.out.println(stack.peek()); // 获取栈顶元素---> 4
        stack.pop(); // 4出栈，栈中剩余1 2 3，栈顶元素为3
        System.out.println(stack.pop()); // 3出栈，栈中剩余1 2 栈顶元素为3
        if(stack.empty()){
            System.out.println("栈空");
        }else{
            System.out.println(stack.size());
        }
    }
}
